import { createRouter, createWebHistory, type RouteRecordRaw } from 'vue-router'
// 布局组件
import DefaultLayout from '@/layouts/DefaultLayout.vue'
import AuthLayout from '@/layouts/AuthLayout.vue'
import EmptyLayout from '@/layouts/EmptyLayout.vue'

// 页面组件
import HomePage from '@/views/HomePage.vue'
import HomeView from '@/views/HomeView.vue'
import LoginPage from '@/views/LoginPage.vue'
import RegisterPage from '@/views/RegisterPage.vue'
import StakingPage from '@/views/StakingPage.vue'
import ExplorePage from '@/views/ExplorePage.vue'
import AccountPage from '@/views/AccountPage.vue'
import SupportPage from '@/views/SupportPage.vue'
import DepositPage from '@/views/DepositPage.vue'
import DepositHistoryPage from '@/views/DepositHistoryPage.vue'
import WithdrawPage from '@/views/WithdrawPage.vue'
import WithdrawHistoryPage from '@/views/WithdrawHistoryPage.vue'

const routes: RouteRecordRaw[] = [
  // 首页 - 使用默认布局
  {
    path: '/',
    component: DefaultLayout,
    children: [
      {
        path: '',
        name: 'Home',
        component: HomePage
      }
    ]
  },

  // 认证相关页面 - 使用空布局
  {
    path: '/auth',
    component: EmptyLayout,
    children: [
      {
        path: 'login',
        name: 'Login',
        component: LoginPage
      },
      {
        path: 'register',
        name: 'Register',
        component: RegisterPage
      }
    ]
  },

  // 应用主要功能 - 使用认证布局
  {
    path: '/app',
    component: AuthLayout,
    meta: { requiresAuth: true },
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: HomeView
      },
      {
        path: 'staking',
        name: 'Staking',
        component: StakingPage
      },
      {
        path: 'explore',
        name: 'Explore',
        component: ExplorePage
      },
      {
        path: 'account',
        name: 'Account',
        component: AccountPage
      },
      {
        path: 'support',
        name: 'Support',
        component: SupportPage
      },
      {
        path: 'deposit',
        name: 'Deposit',
        component: DepositPage
      },
      {
        path: 'deposit/history',
        name: 'DepositHistory',
        component: DepositHistoryPage
      },
      {
        path: 'withdraw',
        name: 'Withdraw',
        component: WithdrawPage
      },
      {
        path: 'withdraw/history',
        name: 'WithdrawHistory',
        component: WithdrawHistoryPage
      }
    ]
  },

  // 兼容旧路由的重定向
  {
    path: '/dashboard',
    redirect: '/app/dashboard'
  },
  {
    path: '/staking',
    redirect: '/app/staking'
  },
  {
    path: '/explore',
    redirect: '/app/explore'
  },
  {
    path: '/account',
    redirect: '/app/account'
  },
  {
    path: '/support',
    redirect: '/app/support'
  },
  {
    path: '/deposit',
    redirect: '/app/deposit'
  },
  {
    path: '/withdraw',
    redirect: '/app/withdraw'
  },
  {
    path: '/login',
    redirect: '/auth/login'
  },
  {
    path: '/register',
    redirect: '/auth/register'
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('user_token')

  // 需要认证的路由
  if (to.matched.some(record => record.meta.requiresAuth)) {
    if (!token) {
      // 没有token，跳转到登录页
      next('/auth/login')
    } else {
      next()
    }
  }
  // 登录和注册页面，如果已登录则跳转到dashboard
  else if ((to.name === 'Login' || to.name === 'Register') && token) {
    next('/app/dashboard')
  }
  // 已登录用户访问公开首页时，重定向到dashboard
  else if (to.path === '/' && token) {
    next('/app/dashboard')
  }
  else {
    next()
  }
})

export default router
